package middleware

import (
	"house/pkg"
	"net/http"
)

type UserAgentMiddleware struct {
}

func NewUserAgentMiddleware() *UserAgentMiddleware {
	return &UserAgentMiddleware{}
}

func (m *UserAgentMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// TODO generate middleware implement function, delete after code implementation
		token := r.Header.Get("token")
		getToken, _ := pkg.GetToken(token)
		//get, _ := cache.Rdb.Get(cache.Ctx, "同步"+pkg.GetOutboundIP()).Result()
		//if get != token {
		//	resp := types.Response{
		//		Code:    500,
		//		Message: "登录失败,当前ip不一致",
		//		Data:    nil,
		//	}
		//	httpx.OkJsonCtx(r.Context(), w, resp)
		//}
		r.ParseForm()
		r.Form.Set("userId", getToken["userId"].(string))
		// Passthrough to next handler if need
		next(w, r)
	}
}
